package com.example.service;

import com.example.entity.Dept;
import com.example.common.PageRequest;
import com.example.common.PageResult;
import com.example.mapper.DeptMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptService {
    @Autowired
    private DeptMapper deptMapper;

    public List<Dept> find(Dept dept) {
        return deptMapper.find(dept);
    }

    public PageResult<Dept> findPage(Dept dept, PageRequest pageRequest) {
        Page<Dept> page = PageHelper.startPage(pageRequest.getPageNumber(), pageRequest.getPageSize());
        deptMapper.find(dept);
        System.out.println(page.getTotal());
        System.out.println(page.getResult());
        return new PageResult<>(page);
    }

    public Dept findOne(Integer id) {
        return deptMapper.findOne(id);
    }

    public void insertOne(Dept dept) {
        LocalDateTime localDateTime = LocalDateTime.now();
        dept.setCreateTime(localDateTime);
        dept.setUpdateTime(localDateTime);
        List<Dept> deptList = List.of(dept);
        deptMapper.insertMany(deptList);
    }

    public void insertMany(List<Dept> deptList) {
        LocalDateTime localDateTime = LocalDateTime.now();
        for (Dept dept : deptList) {
            dept.setCreateTime(localDateTime);
            dept.setUpdateTime(localDateTime);
        }
        deptMapper.insertMany(deptList);
    }

    public void updateMany(List<Integer> ids, Dept dept) {
        LocalDateTime localDateTime = LocalDateTime.now();
        dept.setUpdateTime(localDateTime);
        deptMapper.updateMany(ids, dept);
    }

    public int deleteMany(List<Integer> ids) {
        return deptMapper.deleteMany(ids);
    }
}
